package gui.mvc;

import java.util.List;

import client.RmiClient;

import api.Person;

/**
 * @author Ann Raboschuk
 */
public class Controller
{
   private List<Person> people;

   public Controller()
   {
      people = RmiClient.loadAll();
   }

   public List<Person> getPeople()
   {
      return people;
   }
   
   public void setPeople(List<Person> persons)
   {
      people.clear();
      for (Person person:persons)
      {
         people.add(person);
         RmiClient.updatePerson(person);
      }
   }

   public void deletePerson(int index)
   {
      RmiClient.deletePerson(people.get(index));
      people.remove(index);
   }

   public void editPerson(int index, String[] fields)
   {
      if (fields != null)
      {
         Person changedPerson = people.get(index);
         changedPerson.setSurname(fields[0]);
         changedPerson.setName(fields[1]);
         changedPerson.setAge(Integer.parseInt(fields[2]));
         changedPerson.setSex(fields[3].charAt(0));
         changedPerson.setEmail(fields[4]);
         changedPerson.setPhone(fields[5]);
         changedPerson.setAddress(fields[6]);
         
         RmiClient.updatePerson(changedPerson);
      }
   }

   public void addPerson(String[] fields)
   {
      if (fields != null)
      {
         Person person = new Person(fields[0], fields[1], Integer
               .parseInt(fields[2]), fields[3].charAt(0), fields[4], fields[5],
               fields[6]);
         Long id = RmiClient.addPerson(person);
         person.setId(id);
         people.add(person);         
      }
   }
}
